package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.adapter.ProjectSelectAdapter;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

public class BuildUnitSelectActivity extends Activity {
    private static final String TAG = BuildUnitSelectActivity.class.getName();

    private ProjectSelectAdapter adapter;
    private ListView listView;

    LoadingDialog loadingDialog = null;

    private PtrFrameLayout ptrFrameLayout;

    String prjId;
    IconFontTextview iftv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_unit_select);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        prjId = bundle.getString("prjID");

        initView();
        initEvent();

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        loadList();
    }

    public void initView(){
        listView = (ListView) findViewById(R.id.ll_unitList);
        ptrFrameLayout = (PtrFrameLayout) findViewById(R.id.ptr);

        final StoreHouseHeader header = new StoreHouseHeader(this);
        header.setPadding(0, 15, 0, 0);
        header.initWithString("loading...");
        header.setTextColor(Color.GRAY);
//        StoreHouseHeader storeHouseHeader = new StoreHouseHeader(this);
//        //设置加载数据时显示的字符串，文本只支持0-9，英文字符
//        storeHouseHeader.initWithString("loading...");
//        //设置头布局的背景颜色
//        //storeHouseHeader.setBackgroundColor(Color.BLACK);
//        //设置文本的颜色
//        //storeHouseHeader.setTextColor(Color.WHITE);
//        storeHouseHeader.setDropHeight(200);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header);
    }
    public void initEvent(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Map itemMap = adapter.getItem(arg2);

                String id = (String)itemMap.get("id");
                String name = (String)itemMap.get("name");

                Intent intent=new Intent();

                Bundle bundle = new Bundle();
                bundle.putString("id",id);
                bundle.putString("name",name);

                intent.putExtras(bundle);

                setResult(RESULT_OK, intent);

                finish();
            }
        });
        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            //在这里进行数据加载
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                loadList();
                //handler.sendMessage(handler.obtainMessage(2, null));
            }
        });
    }

    private void loadList(){
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String projectStr = HttpUtil.get("/getBuildUnitListByProjectId?projectid=" + prjId);

                    handler.sendMessage(handler.obtainMessage(1, projectStr));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void back(View view) {
        finish();
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String str = (String)msg.obj;
                    Gson gson=new Gson();
                    Map resultMap = gson.fromJson(str, Map.class);
                    if ((boolean) resultMap.get("success")) {
                        List rows = (List) resultMap.get("rows");

                        adapter = new ProjectSelectAdapter(BuildUnitSelectActivity.this,rows);
                        listView.setAdapter(adapter);
                        listView.deferNotifyDataSetChanged();

                        loadingDialog.close();
                        ptrFrameLayout.refreshComplete();
                    }
                    break;
                case 2:
                    ptrFrameLayout.refreshComplete();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };
}
